package com.ai.alibaba.bailian;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.UUID;

/**
 * 普通对话
 * @author houwenpeng
 */
@RestController
@RequestMapping("/ai")
public class AIChatController {

    private final ChatClient chatClient;

    private ChatMemory chatMemory = new InMemoryChatMemory();

    public AIChatController(ChatClient.Builder builder) {
   
//        this.chatClient = builder.build();

        this.chatClient = builder.defaultAdvisors(new MessageChatMemoryAdvisor(chatMemory, UUID.randomUUID().toString(), 10)).build();
    }

    /**
     * 一次性出来
     * @param message
     * @return
     */
    @GetMapping("/chat")
    public String chat(@Validated String message) {

        System.out.println("提问 : " + message);

        String content = chatClient.prompt().user(message).call().content();

        System.out.println("回答: "+content);

        return content;
    }

    /**
     * 打印的形式进行展示
     * @param input
     * @return
     */
    @GetMapping("/chatFlux")
    public Flux<String> chatFlux(@Validated String input) {

        System.out.println("提问 : " + input);

        Flux<String> content = this.chatClient.prompt().user(input).stream().content();

        System.out.println("回答: "+content);

        return content;
    }

}
